/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaFunciones.java
 *
 * Created on 14/06/2011, 09:30:20 PM
 */

package guiKiosco;

import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import logica.Fabrica;
import logica.IKiosco;
import util.datatypes.DTFuncion;
import util.datatypes.DTPelicula;
import util.exceptions.NoExistePeliculaException;

/**
 *
 * @author bruno
 */
public class ListaFunciones extends javax.swing.JFrame {

    DefaultListModel modelo;
    List<DTFuncion> funciones;
    DTPelicula pelicula;

    public ListaFunciones() {
        initComponents();
        
        this.setSize(1024,768);
    }

    public ListaFunciones(DTPelicula peli) {
        initComponents();
        this.setSize(1024,768);
        this.setLocationRelativeTo(null);

        pelicula = peli;

        lblPelicula.setText(pelicula.getNombre());
        cargarListaFunciones();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pSuperior = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblPelicula = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnSiguiente = new javax.swing.JButton();
        btnSiguiente1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listFunciones = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(1024, 768));

        jLabel1.setFont(new java.awt.Font("Arial", 1, 36));
        jLabel1.setText("Lista de funciones para");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/guiKiosco/resources/star.png"))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Arial", 0, 24));
        jLabel3.setText("Seleccione una");

        lblPelicula.setFont(new java.awt.Font("Arial", 1, 36));
        lblPelicula.setText("[pelicula]");

        javax.swing.GroupLayout pSuperiorLayout = new javax.swing.GroupLayout(pSuperior);
        pSuperior.setLayout(pSuperiorLayout);
        pSuperiorLayout.setHorizontalGroup(
            pSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pSuperiorLayout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56)
                .addGroup(pSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pSuperiorLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblPelicula))
                    .addComponent(jLabel3))
                .addContainerGap(128, Short.MAX_VALUE))
        );
        pSuperiorLayout.setVerticalGroup(
            pSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pSuperiorLayout.createSequentialGroup()
                .addGroup(pSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pSuperiorLayout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(pSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPelicula))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3))
                    .addGroup(pSuperiorLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        getContentPane().add(pSuperior, java.awt.BorderLayout.PAGE_START);

        btnSiguiente.setBackground(new java.awt.Color(242, 241, 240));
        btnSiguiente.setFont(new java.awt.Font("Arial", 0, 48));
        btnSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/guiKiosco/resources/arrow_right.png"))); // NOI18N
        btnSiguiente.setBorder(null);
        btnSiguiente.setBorderPainted(false);
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });

        btnSiguiente1.setBackground(new java.awt.Color(242, 241, 240));
        btnSiguiente1.setFont(new java.awt.Font("Arial", 0, 48));
        btnSiguiente1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/guiKiosco/resources/arrow_left.png"))); // NOI18N
        btnSiguiente1.setBorder(null);
        btnSiguiente1.setBorderPainted(false);
        btnSiguiente1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguiente1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(614, Short.MAX_VALUE)
                .addComponent(btnSiguiente1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSiguiente, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(32, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnSiguiente1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSiguiente, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        listFunciones.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        listFunciones.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listFunciones.setSelectionBackground(new java.awt.Color(31, 90, 221));
        listFunciones.setSelectionForeground(new java.awt.Color(254, 254, 254));
        jScrollPane1.setViewportView(listFunciones);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSiguiente1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguiente1ActionPerformed

        ListaPeliculas frm = new ListaPeliculas();
        frm.setVisible(true);

        this.setVisible(false);
    }//GEN-LAST:event_btnSiguiente1ActionPerformed

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed

        IKiosco i = Fabrica.getIKiosco();

        if (listFunciones.getSelectedValue() != null) {

            DTFuncion func = funciones.get(listFunciones.getSelectedIndex());

            if(i.getChequearHoraFuncion(func)){

                ReservaEntradas frm = new ReservaEntradas(pelicula, func);
                frm.setVisible(true);

                this.setVisible(false);

            }else{
                JOptionPane.showMessageDialog(null, "Las reservas pueden ser realizadas hasta media hora antes de la funcion");
            }

            
        }else{

            JOptionPane.showMessageDialog(null, "Debe seleccionar un funcion para continuar");
        }
    }//GEN-LAST:event_btnSiguienteActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListaFunciones().setVisible(true);
            }
        });
    }

    private void cargarListaFunciones(){
        try {

            IKiosco i = Fabrica.getIKiosco();

            funciones = i.listarFunciones(pelicula.getId());
            
            modelo = new DefaultListModel();

            for (DTFuncion dtFuncion : funciones) {

                String aux = "Horario: " + i.obtenerHoraString(dtFuncion.getHoraInicio()) + "          Entradas disponibles: " + i.contarEntradas(dtFuncion)/*dtFuncion.getEntradasDisponibles()*/;
                modelo.addElement(aux);
            }

            listFunciones.setModel(modelo);

        } catch (NoExistePeliculaException ex) {
            JOptionPane.showMessageDialog(null, "Error al cargar la lista de funciones");
        }
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JButton btnSiguiente1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblPelicula;
    private javax.swing.JList listFunciones;
    private javax.swing.JPanel pSuperior;
    // End of variables declaration//GEN-END:variables

}
